package com.music.wangyi.model

import android.util.Log
import android.view.View
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.music.wangyi.Base.BaseViewModel
import com.music.wangyi.Bean.dataConvert
import com.music.wangyi.R
import com.music.wangyi.Services.LoginServices
import com.music.wangyi.utils.request
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.lang.Exception


class LoginModel: BaseViewModel() {

    val net by lazy { request.instance() }

    var userInfo = MutableLiveData<String>()

    var username = ObservableField<String>()
    var password = ObservableField<String>()

    fun goToIphoneLoginFragment(view:View){
        navigate(view,R.id.iphoneloginFragment)
    }

    fun login(view: View){
        Log.w("username",username.get())
        Log.w("password",password.get())
        viewModelScope.launch {
            try {
                /*withContext表示挂起块  配合Retrofit声明的suspend函数执行 该块会挂起直到里面的网络请求完成 最一行就是返回值*/
                val data = withContext(Dispatchers.IO) {

                    /*dataConvert扩展函数可以很方便的解析出我们想要的数据  接口很多的情况下下可以节省不少无用代码*/
                    net.create(LoginServices::class.java).getIphoneLogin("123","123").dataConvert()
                }
                userInfo.value = data
            }catch (e:Exception){

            }
        }
    }
}